package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.Post;
import com.example.demo.mapper.PostMapper;
import com.example.demo.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

/**
 * 帖子服务实现类
 * 提供帖子的增删改查、分页查询、浏览量和点赞量统计等功能
 */
@Service
public class PostServiceImpl implements PostService {

    @Autowired
    private PostMapper postMapper;

    /**
     * 保存帖子实体
     * @param entity 帖子实体
     * @return 保存是否成功
     */
    @Override
    public boolean save(Post entity) {
        return postMapper.insert(entity) > 0;
    }

    @Override
    public boolean saveBatch(Collection<Post> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean saveOrUpdateBatch(Collection<Post> entityList, int batchSize) {
        return false;
    }

    /**
     * 根据ID更新帖子信息
     * @param entity 包含更新信息的帖子实体
     * @return 更新是否成功
     */
    @Override
    public boolean updateById(Post entity) {
        return postMapper.updateById(entity) > 0;
    }

    @Override
    public boolean updateBatchById(Collection<Post> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean saveOrUpdate(Post entity) {
        return false;
    }

    @Override
    public Post getOne(Wrapper<Post> queryWrapper, boolean throwEx) {
        return null;
    }

    @Override
    public Map<String, Object> getMap(Wrapper<Post> queryWrapper) {
        return Map.of();
    }

    @Override
    public <V> V getObj(Wrapper<Post> queryWrapper, Function<? super Object, V> mapper) {
        return null;
    }

    @Override
    public BaseMapper<Post> getBaseMapper() {
        return null;
    }

    @Override
    public Class<Post> getEntityClass() {
        return null;
    }

    /**
     * 根据ID删除帖子
     * @param id 帖子ID
     * @return 删除是否成功
     */


    /**
     * 根据ID获取帖子信息
     * @param id 帖子ID
     * @return 帖子实体
     */

    /**
     * 批量保存帖子
     * @param entityList 帖子实体集合
     * @return 保存是否成功
     */
    @Override
    public boolean saveBatch(Collection<Post> entityList) {
        entityList.forEach(postMapper::insert);
        return true;
    }

    /**
     * 分页获取帖子列表
     * @param pageNum 页码
     * @param pageSize 每页数量
     * @param categoryId 分类ID，可为null
     * @return 分页后的帖子列表
     */
    @Override
    public IPage<Post> getPostPage(Integer pageNum, Integer pageSize, Long categoryId) {
        System.out.println(pageNum+pageSize+categoryId+"6asdfghjkl");
        LambdaQueryWrapper<Post> queryWrapper = new LambdaQueryWrapper<>();
        if (categoryId != null) {
            System.out.println("========================"+pageNum+pageSize+categoryId+"6asdfghjkl");
            queryWrapper.eq(Post::getCategoryId, categoryId);
        }
        queryWrapper.orderByDesc(Post::getCreatedAt);
        return postMapper.selectPage(new Page<>(pageNum, pageSize), queryWrapper);
    }

    /**
     * 增加帖子浏览量
     * @param postId 帖子ID
     * @return 更新是否成功
     */
    @Override
    public boolean incrementViewCount(Long postId) {
        Post post = postMapper.selectById(postId);
        if (post != null) {
            post.setViewCount(post.getViewCount() + 1);
            return postMapper.updateById(post) > 0;
        }
        return false;
    }

    /**
     * 增加帖子点赞量
     * @param postId 帖子ID
     * @return 更新是否成功
     */
    @Override
    public boolean incrementLikeCount(Long postId) {
        Post post = postMapper.selectById(postId);
        if (post != null) {
            post.setLikeCount(post.getLikeCount() + 1);
            return postMapper.updateById(post) > 0;
        }
        return false;
    }

    /**
     * 获取帖子详情，同时增加浏览量
     * @param postId 帖子ID
     * @return 帖子实体
     */
    @Override
    public Post getPostDetail(Long postId) {
        Post post = postMapper.selectById(postId);
        if (post != null) {
            // 增加浏览量
            incrementViewCount(postId);
        }
        return post;
    }

    @Override
    public List<Post> all() {
        return postMapper.selectList1();
    }
}